var util = require('../../utils/util.js');
var api = require('../../config/api.js');
var user = require('../../services/user.js');
var app = getApp();

Page({
    data: {
        info:[],
        category:[],
        pageSize:20,
        pageNo:1,
        hasMore:true,
        lon:0,
        lat:0
    },
    onPullDownRefresh: function () {
        this.getData(this.data.lat,this.data.lon)
        this.getCategoryInfo()
    },
    onReachBottom: function(){
        let that = this
        this.loadMore(that.data.lat,that.data.lon)
    },
    onLoad: function(options) {
        // 页面初始化 options为页面跳转所带来的参数
        let that = this
        wx.getLocation({
            type: 'wgs84',
            success (res) {
                that.setData({
                    lon:res.longitude,
                    lat:res.latitude
                })
                that.getData(that.data.lat,that.data.lon)
            },
            fail:function(err){
                that.getData(that.data.lat,that.data.lon)
            }
        })
        this.getCategoryInfo()
    },
    onCall(e){
        wx.makePhoneCall({
          phoneNumber: e.currentTarget.dataset.tel,
        })
    },
    getData: function(lat,lon){
        let that = this;
        wx.showLoading({
          title: '加载中...',
        })
        that.setData({
            pageNo: 1,
            hasMore:true
        });
        util.request(api.surroundings.getIndexMerchant+'?pageSize='+that.data.pageSize+'&pageNo='+that.data.pageNo+'&lat='+lat+'&lon='+lon, {},'get').then(res=>{
            that.setData({
                info:res.result
            })
            if (res.result.length < that.data.pageSize) {
                that.setData({
                    hasMore: false,
                })
            }
            wx.hideLoading()
            wx.stopPullDownRefresh();
        }).catch(err=>{
            console.log(err)
        })

    },
    loadMore: function(lat,lon) {
        var that = this;
        if (!this.data.hasMore) return
        wx.showLoading({
            title: '加载中...',
        })
        util.request(api.surroundings.getIndexMerchant+'?pageSize='+that.data.pageSize+'&pageNo='+(++that.data.pageNo)+'&lat='+lat+'&lon='+lon, {},'get').then(res=>{
                that.setData({
                    info: that.data.info.concat(res.result)
                });
                if (res.result.length < that.data.pageSize) {
                    that.setData({
                        hasMore: false,
                    })
                }
                wx.hideLoading()
        }).catch(err=>{
            console.log(err)
        })
    },
    getCategoryInfo(){
        let that = this;
        util.request(api.surroundings.getMerchantCategoryInfoList,{},'post').then(res=>{
            that.setData({
                category:res.result
            })
            wx.stopPullDownRefresh()
        }).catch(err=>{
            console.log(err)
            wx.stopPullDownRefresh()
        })
    },
    onReady: function() {

    },
    onShow: function() {
        if(app.globalData.cartNum > 0){
            wx.setTabBarBadge({
                index: 3,
                text: app.globalData.cartNum.toString()
            })
        }else{
            wx.removeTabBarBadge({index:3})
        }

    },
    onHide: function() {
        // 页面隐藏

    },
    onUnload: function() {
        // 页面关闭
    }

})
